using System.Text.RegularExpressions;

namespace Codingly.SvnExternalUpdater.Model
{
    internal class ExternalTemplate
    {
        #region Readonly & Static Fields

        private readonly ExternalType type;

        #endregion

        #region Fields

        private GroupCollection groups;

        #endregion

        #region Constructors

        public ExternalTemplate(ExternalType type)
        {
            this.type = type;
        }

        #endregion

        #region Instance Properties

        public GroupCollection Groups
        {
            get { return this.groups; }
        }

        public ExternalType Type
        {
            get { return this.type; }
        }

        #endregion

        #region Instance Methods

        public bool Match(string external)
        {
            external = CleanExternal(external);
            this.groups = GetGroups(external, ExternalTypeRegex.GetRegex(this.type));
            return this.groups != null;
        }

        #endregion

        #region Class Methods

        private static string CleanExternal(string external)
        {
            external = external.Trim();
            external = external.Replace('\\', '/');
            return external;
        }

        private static GroupCollection GetGroups(string external, Regex regex)
        {
            GroupCollection result = null;
            Match match = regex.Match(external);
            if (match.Success)
            {
                result = match.Groups;
            }
            return result;
        }

        #endregion
    }
}